package com.greedy.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.greedy.bean.dto.UserDTO;
import com.greedy.bean.pojo.Follow;
import com.greedy.bean.pojo.User;
import com.greedy.interceptor.UserHolder;
import org.n3r.eql.Eql;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-05-01
 * Description:
 */
@Service
public class FollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void follow(Long followUserId, Boolean isFollow) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        // 1.判断到底是关注还是取关
        if (isFollow) {
            // 2.关注，新增数据
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            Integer isSuccess = new Eql().insert("insertFollow").params(follow).returnType(Integer.class).execute();
            if (isSuccess == 1) {
                // 把关注用户的id，放入redis的set集合 sadd userId followerUserId
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        } else {
            // 3.取关，删除 delete from tb_follow where user_id = ? and follow_user_id = ?
            HashMap<String, Long> eqlMap = new HashMap<>(2);
            eqlMap.put("userId", userId);
            eqlMap.put("followUserId", followUserId);
            Integer isSuccess = new Eql().delete("removeFollow").params(eqlMap).returnType(Integer.class).execute();
            if (isSuccess == 1) {
                // 把关注用户的id从Redis集合中移除
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }
        }
    }

    public Boolean isFollow(Long followUserId) {
        // 1.获取登录用户
        Long userId = UserHolder.getUser().getId();
        // 2.查询是否关注 select count(*) from tb_follow where user_id = ? and follow_user_id = ?
        HashMap<String, Long> eqlMap = new HashMap<>(2);
        eqlMap.put("userId", userId);
        eqlMap.put("followUserId", followUserId);
        Integer count = new Eql().selectFirst("isFollow").params(eqlMap).returnType(Integer.class).execute();
        // 3.判断
        return count > 0;
    }

    public List<UserDTO> followCommons(Long id) {
        // 1.获取当前用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        // 2.求交集
        String key2 = "follows:" + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
        if (intersect == null || intersect.isEmpty()) {
            // 无交集
            return Collections.emptyList();
        }
        // 3.解析id集合
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 4.查询用户
        String sql = "select *\n" +
                "from user\n" +
                "where ID in (" + StrUtil.join(",", ids) + ")\n";
        List<User> users = new Eql().returnType(User.class).execute(sql);
        return users
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
    }
}
